package com.leetcode.contest;

/**
 * 6078. 重排字符形成目标字符串
 */
public class Solution {
    public int rearrangeCharacters(String s, String target) {
        int[] chars = new int[26];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            chars[c - 'a']++;
        }

        int[] chars1 = new int[26];
        for (int i = 0; i < target.length(); i++) {
            char c = target.charAt(i);
            chars1[c - 'a']++;
        }

        int min = Integer.MAX_VALUE;
        for (int j = 0; j < chars1.length; j++) {
            int i = chars1[j];
            if (i > 0) {
                if (chars[j] == 0) {
                    return 0;
                }
                min = Math.min(min, chars[j] / i);
            }
        }

        return min;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        String s = "abcba";
        String target = "abc";

        int i = solution.rearrangeCharacters(s, target);

        System.out.println(i);
    }
}
